package com.controller;

import com.service.EmpService;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/emp")
public class EmpNameServlet extends HttpServlet {


    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        int id = 2;
        ApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(req.getServletContext());

        EmpService empService = context.getBean("empService", EmpService.class);
        String name = empService.getNameById(id);
        req.setAttribute("empname", name);
        req.getRequestDispatcher("/WEB-INF/emp.jsp").forward(req, resp);
    }
}
